/***************************************************************************
 *  Copyright (c) 2005 Chih-Chung Chang                                    *
 *  Parts copyright (c) 2006 Michael Olson                                 *
 *  Copyright (C) 2007-2008 by Fehmi "Canavar" SAGLAM                      *
 *  canavar@fehmicans.net                                                  *
 *                                                                         *
 *  This file is part of sTerm.                                            *
 *                                                                         *
 *  sTerm is free software; you can redistribute it and/or modify          *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation; either version 3 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  sTerm is distributed in the hope that it will be useful,               *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>   *
 ***************************************************************************/

#ifndef _sterm_h__
#define _sterm_h__

#include "GP2X.h"
#include "stermkeyboard.h"
#include <map>

#define SELECTOR_HEIGHT 3
#define GP2X_CLIENT_HEIGHT _gp2xClientHeight
#define GP2X_CLIENT_WIDTH  _gp2xClientWidth
int _gp2xClientWidth;
int _gp2xClientHeight;

#define PERIOD 90000
#define REPEAT_COUNTER1 1
#define REPEAT_COUNTER2 3

//#define _DEBUG

//#define SCREEN_WIDTH (GP2X_WIDTH/FONT_WIDTH)
int _screenWidth;
//#define SCREEN_HEIGHT (GP2X_HEIGHT/FONT_HEIGHT)
int _screenHeight;
//#define CLIENT_WIDTH SCREEN_WIDTH
int _clientWidth;
//#define CLIENT_HEIGHT SCREEN_HEIGHT - SELECTOR_HEIGHT
int _clientHeight;

enum {
    INITIAL,
    LBRACKET,
    IN_ARG1,
    IN_ARG2,
    LBRACKET_Q,
    IN_ARG1_Q
};

SDL_Surface *screen, *bg, *sterm;
SDL_Joystick *joystick;
StermKeyboard *stermKeyboard = NULL;
StermButton *shiftButton;
StermButton *ctrlButton;
StermButton *altButton;
 
const char *CONFIG_FILE;
const char *BG_IMAGE;

char** EXTRA_ARGV;
int    EXTRA_ARGC;


int master;	// pty master fd
/*bool debug, capture*/;
int last_pos = -1;	// last shown cursor position
int cur_pos; // current cursor position
int no_event2 = REPEAT_COUNTER2;

/* ESC commands */
bool blinking_mode;
bool halfbright_mode;
bool bold_mode;
bool reverse_mode;
bool dark_mode;
bool vkeyboard = true;
bool usbkeyboard = true;

/* color handling */
const int R = 0xf800;
const int G = 0x07e0;
const int B = 0x001f;
const int colors[] = { 0, R, G, R|G, B, R|B, G|B, R|G|B };
int def_bg_color = colors[0]; // default background color
int def_fg_color = colors[7]; // default foreground color
int bg_color = def_bg_color; // background color
int fg_color = def_fg_color; // foreground color
int cursor_color = G;

/* indicators */
bool alt_down = false;
bool ctrl_down = false;
bool shift_down = false;
bool js_down = false;
bool caps_lock = false;

typedef map<char, char> charMap;
typedef charMap::value_type charValType;
charMap cm;

static void init();
static void exit_func();
static void show_cursor();
static void hide_cursor();
static void cmd_cr();
static void display_string(const char *buf, int n);
static void display_string(string);
static bool process_esc_mode(unsigned char c);
static void process_direction(int type, bool js_down);
static void insert_key(string);
static void insert_key(char);
static int load_config();
#endif
